package com.luoke.dao.impl;


import com.luoke.dao.BagDao;
import com.luoke.pojo.dto.BagDTO;
import com.luoke.util.DatabaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : 叶非凡
 * @date : 2024/11/14 17:05
 */
public class BagDaoImpl implements BagDao {

    @Override
    public List<BagDTO> getUserBagByUsername(String userName) {

        String sql = "SELECT `user`.`name` AS username,`goods`.`name` AS goodsName,`bag`.quantity,`goods`.type,`goods`.description,`goods`.`value` FROM `bag` LEFT JOIN `goods` ON bag.goods_id = goods.id LEFT JOIN `user` ON `bag`.user_id = `user`.id WHERE `user`.id = (SELECT id FROM `user` WHERE `name` = ?);";


        List<BagDTO> bagItems = new ArrayList<>();

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            // 设置参数
            stmt.setString(1, userName);

            // 执行查询
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    // 获取查询结果
                    String username = rs.getString("username");
                    String goodsName = rs.getString("goodsName");
                    int quantity = rs.getInt("quantity");
                    String type = rs.getString("type");
                    String description = rs.getString("description");
                    String value = rs.getString("value");

                    // 创建 BagDTO 对象并添加到列表
                    BagDTO bagItem = new BagDTO(username, goodsName, quantity,type,description,value);
                    bagItems.add(bagItem);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return bagItems;

    }

    @Override
    public void deleteBagQuantityByGoodsNameAndUserId(String goodsName, Long id) {

        String sql  = "DELETE FROM bag WHERE  user_id = ? AND goods_id = (SELECT id FROM goods WHERE `name` = ?);";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)){

            stmt.setLong(1,id);
            stmt.setString(2,goodsName);
            stmt.executeUpdate();


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }



    }

    @Override
    public void updateBagQuantityByGoodsNameAndUserId(String goodsName, Long id, Integer quantity) {
        String sql = "UPDATE bag SET quantity = ? WHERE user_id = ? AND goods_id = (SELECT id FROM goods WHERE `name` = ?);";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)){

            stmt.setInt(1,quantity-1);
            stmt.setLong(2,id);
            stmt.setString(3,goodsName);

            stmt.executeUpdate();


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }
}
